package com.hyp.controller;

import com.hyp.pojo.Favorites;
import com.hyp.pojo.Product;
import com.hyp.pojo.Result;
import com.hyp.service.FavoritesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j //显示日志消息
@RestController
@RequestMapping("/wx/")
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    //添加收藏
    @PostMapping("/favorite/{userId}/{productId}")
    public Result toggleFavorite(@PathVariable Integer userId, @PathVariable Integer productId, @RequestBody Map<String, Boolean> request) {
        boolean isFavorite = request.get("isFavorite");
        try {
            favoritesService.toggleFavorite(userId,productId, isFavorite);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    //检查是否收藏
    @GetMapping("/favorite/check/{userId}/{productId}")
    public Result checkFavorite(@PathVariable Integer userId, @PathVariable Integer productId) {
        boolean isFavorite = favoritesService.isFavorite(userId, productId);
        return Result.success(Map.of("isFavorite", isFavorite));
    }
    //获取收藏列表
    @GetMapping("/favoriteList/{userId}")
    public Result favoriteList(@PathVariable Integer userId){
        List<Product> favoriteList = favoritesService.favoriteList(userId);
        return Result.success(favoriteList);
    }
}
